package Search.search2020;

/**
 * 二分查找
 *
 * @author leiwenkang
 * @date 2020/3/29
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] array = {1, 2, 4, 5, 25, 63, 72, 81};
        System.out.println(binarySearch(array, 25));
    }

    public static int binarySearch(int[] array, int target) {
        if (array == null || array.length == 0) {
            return -1;
        }

        int low = 0;
        int high = array.length - 1;
        while (low <= high) {
            int mid = (low + high) >> 1;
            if (array[mid] == target) {
                return mid;
            } else if (array[mid] < target) {
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return -1;
    }
}
